﻿Public Class perUpdate
    Inherits perPadrao
#Region "Variáveis"

    Private UpdateCnn As New System.Data.OleDb.OleDbConnection
    Private UpdateCMD As New System.Data.OleDb.OleDbCommand
    Private UpdateDa As New System.Data.OleDb.OleDbDataAdapter
    Private prvCaminhoUpdate As String
    Private prvVersaoAtual As Long

    Private objConfig As New perConfiguracoes
#End Region

#Region "Propriedades públicas"
    Public WriteOnly Property CaminhoUpdate()
        Set(ByVal value)
            prvCaminhoUpdate = value
        End Set

    End Property
#End Region

#Region "Métodos Públicos"
    Public Function ExecutarUpdate() As Boolean
        Dim dsAtualizacao As DataSet
        Dim VersaoFinal As Long
        Dim bRetorno As Boolean = True

        Try
            If (ConectarUpdate()) Then
                prvVersaoAtual = objConfig.VersaoAtual
                dsAtualizacao = Me.Atualizacoes

                If (Not dsAtualizacao Is Nothing And dsAtualizacao.Tables(0).Rows.Count > 0) Then
                    With dsAtualizacao.Tables(0)
                        For Each drDados As DataRow In .Rows
                            MyBase.executarAcao(perConversao.nuloParaVazio(drDados.Item("Script").ToString))
                            VersaoFinal = perConversao.nuloParaZero(drDados.Item("Versao"))
                        Next
                    End With

                    Call objConfig.GravarConfiguracoes(VersaoFinal, "Versao")
                End If
                Kill(prvCaminhoUpdate)

            End If
        Catch ex As Exception
            bRetorno = False
            Throw New Exception("Exceção no método 'ExecutarUpdate'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
        Return bRetorno

    End Function
#End Region

#Region "Métodos Privados"

    Private Function ConectarUpdate() As Boolean

        Try
            If Me.UpdateCnn.State = ConnectionState.Closed Then
                UpdateCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= DSUpdate.mdb"
                UpdateCnn.Open()
                UpdateCMD.Connection = UpdateCnn
                UpdateCMD.CommandType = CommandType.Text
            End If
            Return True
        Catch ex As Exception

            Throw New Exception("Exceção no método 'ConectarUpdate'." & Environment.NewLine & Environment.NewLine & ex.Message)
            Return False
        End Try

    End Function

    Private Function Atualizacoes() As DataSet
        Dim sSql As String

        Try
            sSql = "SELECT Update.Versao, Update.Script" & vbCrLf
            sSql &= " FROM [Update]" & vbCrLf
            sSql &= "WHERE Versao > " & prvVersaoAtual

            Return Me.executarConsultaUpdate(sSql, "Update")

        Catch ex As Exception
            Throw New Exception("Exceção no método 'Atualizacoes'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Protected Function executarConsultaUpdate(ByRef sSql As String, _
                                        ByVal sNomeTabela As String) As DataSet
        Dim dsRetorno As New DataSet

        Try
            Me.ConectarUpdate()
            Me.UpdateCMD.CommandText = sSql
            Me.UpdateDa.SelectCommand = Me.UpdateCMD
            Me.UpdateDa.Fill(dsRetorno, sNomeTabela)

            Return dsRetorno

        Catch ex As Exception

            Throw New Exception("Ocorreu um erro ao tentar executar a sentença SQL de consulta no Banco de Dados." & Environment.NewLine & Environment.NewLine & ex.Message)

        Finally

            Me.FecharConexaoUpdate()

        End Try
    End Function

    Private Sub FecharConexaoUpdate()

        Try

            UpdateCnn.Close()

        Catch ex As Exception
            Throw New Exception("Exceção no método 'FecharConexaoUpdate'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

#End Region

End Class
